<?php
namespace app\api\controller;

use think\Request;

class Build extends BaseController
{
    public function create()
    {
        $result = 0;
        if (Request::instance()->isPost()) {
            $user_id = input('post.user_id');
            $build_address = input('post.build_address');
            $build_electricity_price = input('post.build_electricity_price');
            $build_water_price = input('post.build_water_price');

            $build_model = model('Build');
            $build_model->data([
                'user_id' => $user_id,
                'build_address' => $build_address,
                'build_electricity_price' => $build_electricity_price,
                'build_water_price' => $build_water_price
            ]);
            $result = $build_model->save();
        }

        return $result;
    }

    public function update()
    {
        $result = 0;

        if (Request::instance()->isPost()) {
            $build_id = input('post.build_id');
            $build_address = input('post.build_address');
            $build_electricity_price = input('post.build_electricity_price');
            $build_water_price = input('post.build_water_price');

            $build_model = model('Build');
            $result = $build_model->save([
                'build_address' => $build_address,
                'build_electricity_price' => $build_electricity_price,
                'build_water_price' => $build_water_price
            ],['build_id' => $build_id]);
        }

        return $result;
    }

    public function save()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $build_id = input('post.build_id');

            $result = isset($build_id) && $build_id ? $this->update() : $this->create();
            $data['status'] = $result ? 200 : 400;
        }

        return json($this->dataReturn($data));
    }

    public function ls()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $user_id = input('post.user_id');

            $build_model = model('Build');

            $build = $build_model->all(['user_id' => $user_id, 'is_deleted' => '0']);

            $data['status'] = 200;
            $data['build'] = $build;

        }

        return json($this->dataReturn($data));
    }

    public function get()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $build_id = input('post.build_id');

            $build_model =model('Build');

            $data['status'] = 200;
            $data['build'] = $build_model->get(['build_id' => $build_id, 'is_deleted' => '0']);
        }

        return json($this->dataReturn($data));
    }

    public function remove()
    {
        $data = [];

        if (Request::instance()->isPost()) {
            $build_id = input('post.build_id');

            $build_model = model('Build');
            $build_model->save([
                'is_deleted' => '1'
            ], ['build_id' => $build_id]);

            $data['status'] = 200;
        }

        return json($this->dataReturn($data));
    }
}